#include <iostream>
#include <fstream>
#include <vector>
#include <cstdio>

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

using namespace std;

struct Vertex {
    glm::vec3 position;
    glm::vec3 normal;
    glm::vec2 texCoord;
};

struct Face {
    unsigned int v1[3];
    unsigned int v2[3];
    unsigned int v3[3];
};

void parseObj(char const *path) {
    ifstream is(path, ios::in);
    if(!is.is_open()) {
        std::cout<<"Failed to open"<<path<<std::endl;
        return;
    }
    vector<glm::vec3> positions;
    vector<glm::vec3> normals;
    vector<glm::vec2> texCoords;
    vector<Face> faces;
    while (!is.eof()) {
        char buf[1024] = {0};
        is.getline(buf, sizeof(buf));
        printf("%s\n", buf);
        
        if (strncmp(buf, "v ", 2) == 0) {
            glm::vec3 position;
            sscanf(buf, "v %6f %6f %6f", &position.x, &position.y, &position.z);
            std::cout<<"("<<position.x<<", "<<position.y<<", "<<position.z<<")"<<std::endl;
            positions.push_back(position);
        }
        if (strncmp(buf, "vn ", 3) == 0) {
            glm::vec3 normal;
            sscanf(buf, "vn %6f %6f %6f", &normal.x, &normal.y, &normal.z);
            std::cout<<"("<<normal.x<<", "<<normal.y<<", "<<normal.z<<")"<<std::endl;
            normals.push_back(normal);
        }
        if (strncmp(buf, "vt ", 3) == 0) {
            glm::vec2 texCoord;
            sscanf(buf, "vt %6f %6f", &texCoord.x, &texCoord.y);
            std::cout<<"("<<texCoord.x<<", "<<texCoord.y<<")"<<std::endl;
            texCoords.push_back(texCoord);
        }
        if (strncmp(buf, "f ", 2) == 0) {
            Face face;
            sscanf(buf, "f %d/%d/%d %d/%d/%d %d/%d/%d", &face.v1[0], &face.v1[1], &face.v1[2], &face.v2[0], &face.v2[1], &face.v2[2], &face.v3[0], &face.v3[1], &face.v3[2]);
            std::cout<<"("<<face.v1[0]<<", "<<face.v1[1]<<", "<<face.v1[2]<<")|"<<"("<<face.v2[0]<<", "<<face.v2[1]<<", "<<face.v2[2]<<")|"<<"("<<face.v3[0]<<", "<<face.v3[1]<<", "<<face.v3[2]<<")"<<std::endl;
            faces.push_back(face);
        }
    }
    vector<Vertex> vertices;
    vector<unsigned int> indices;
    /*
    for (int i = 0, l = faces.size(); i < l; ++i) {
        Vertex vertex = {positions(faces[i].v1[0]), normals(faces[i].v1[1]), texCoords(faces[i].v1[2]};
        vertices.push_back(vertex);
        indices.push_back(faces[i].v1[0]);
    }
    */
}

int charNum(char const *str, char characher) {
    int result = 0;
    char *p = (char *)str;
    while (p[0] != '\0') {
        if (p[0] == characher) {
            result++;
        }
        p++;
    }
    return result;
}

int main(int arc, char *argv[]) {
    // parseObj("res/cube.obj");
    int num = charNum("f 1/1/1 2/2/1 3/3/1 4/4/1", ' ');
    printf("num: %d\n", num);
    return 0;
}
